<template>
  <div class="app-container">
    <el-card
      class="filter-container"
      shadow="never"
    >
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          @click="handleResetSearch()"
          style="float:right;margin-right: 15px"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="渠道：">
            <el-select
              v-model="listQuery.channel"
              placeholder="APP"
              clearable
              class="input-width"
            >
              <el-option
                v-for="item in methodType"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="位置：">
            <el-select
              v-model="listQuery.position"
              placeholder="请选择"
              class="input-width"
            >
              <el-option
                v-for="(item,index) in palaceType"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <!-- 数据列表 -->
    <el-card
      class="operate-container"
      shadow="never"
    >
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>

    <!-- 数据展示 -->
    <div class="table-container">
      <el-table
        ref="orderTable"
        :data="bannerList"
        v-loading="listLoading"
        style="width: 100%;"
        @selection-change="handleSelectionChange"
        empty-text="请选择位置后，点击查询搜索"
        border
      >
        <el-table-column
          label="排序"
          align="center"
          width="100"
        >
          <template slot-scope="scope">{{ scope.row.sort }}</template>
        </el-table-column>
        <el-table-column
          label="图片"
          width="240"
          align="center"
        >
          <template slot-scope="scope">
            <img
              style="height: 80px"
              :src="scope.row.bannerImage"
            >
          </template>
        </el-table-column>
        <el-table-column
          label="状态"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.status==="1" ? "生效中" :"未生效" }}</template>
        </el-table-column>
        <el-table-column
          label="生效日期"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.startTime | formatCreateTime }}</template>
        </el-table-column>
        <el-table-column
          label="失效日期"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.endTime | formatCreateTime }}</template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleEditSort(scope.$index, scope.row)"
            >设置排序
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleRelective(scope.$index, scope.row)"
            >关联内容</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 排序对话框 -->
    <el-dialog
      title="设置排序"
      :visible.sync="sortDialogVisible"
      width="40%"
    >
      <el-form
        :model="sortDialogData"
        label-width="150px"
      >
        <el-form-item label="序号">
          <el-select
            v-model="sortDialogData.sort"
            placeholder="1"
            class="input-width"
          >
            <el-option
              v-for="item in sortList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button
          @click="sortDialogVisible = false"
          size="small"
        >取 消</el-button>
        <el-button
          type="primary"
          @click="handleUpdateSort"
          size="small"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>

</template>

<script>
import { bannerList, saveUpdate } from '@/api/banner'
import { formatDate } from '@/utils/date'

const defaultListQuery = {
  orderBy: '',
  position: null,
  channel: ''
}
export default {
  data () {
    return {
      total: null,
      methodType: [
        {
          value: '1',
          label: 'APP'
        },
        {
          value: '2',
          label: '小程序'
        },
        {
          value: '3',
          label: '公众号'
        }
      ],
      palaceType: [
        {
          value: '01',
          label: '首页\\头部banner'
        },
        {
          value: '02',
          label: '学习首页\\头部banner'
        }
      ],
      sortList: [
        {
          value: '1',
          label: '1'
        },
        {
          value: '2',
          label: '2'
        },
        {
          value: '3',
          label: '3'
        },
        {
          value: '4',
          label: '4'
        },
        {
          value: '5',
          label: '5'
        }
      ],
      listQuery: Object.assign({}, defaultListQuery),
      bannerList: null,
      // 选择的复选框
      multipleSelection: [],
      listLoading: false,
      // 排序对话框
      sortDialogVisible: false,
      sortDialogData: { sort: 0, id: null }
    }
  },
  created () {
    // this.getList();
  },
  methods: {
    // 重置
    handleResetSearch () {
      this.listQuery = Object.assign({}, defaultListQuery)
    },
    // 筛选搜索
    handleSearchList () {
      this.getList()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    // 排序
    handleEditSort (index, row) {
      this.sortDialogVisible = true
      this.sortDialogData.sort = row.sort
      this.sortDialogData.id = row.id
    },
    // 保存排序
    handleUpdateSort () {
      this.$confirm('是否要修改序号?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        saveUpdate(this.sortDialogData).then((response) => {
          this.sortDialogVisible = false
          this.getList()
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
        })
      })
    },
    // 关联内容
    handleRelective (index, row) {
      this.$router.push({
        path: '/cont-ms/relective',
        query: { id: row.id, channel: row.channel, position: row.position }
      })
    },
    getList () {
      console.log(this.listQuery)
      this.listLoading = true
      this.listQuery.orderBy = 'sort asc'
      bannerList(this.listQuery).then((response) => {
        this.listLoading = false
        console.log(response.data)
        this.bannerList = response.data.list
        // this.total = response.data.total
        // this.listLoading = false
        // this.list = response.data.list.map((item) => {
        //   item.cityName = '北京'
        //   return item
        // })
        // this.total = response.data.total
        // console.log(this.total)
      })
    }
  },
  filters: {
    formatCreateTime (time) {
      if (time == null || time === '') {
        return 'N/A'
      }
      const date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  }
}
</script>

<style scoped>
</style>
